Palindrome Number

Leetcode practice log

Posted by Rui on 16-09-2021
Estimated Reading Time 1 Minutes
Words 138 In Total
Viewed Times

Palindrome Number

Given an integer x, return true if x is palindrome integer.

An integer is a palindrome when it reads the same backward as forward. For example, 121 is palindrome while 123 is not.

Example 1:

Input: x = 121
Output: true

First Try

the same as Reverse Interger, use number.toString().split(‘’).reverse().join(‘’)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
/**
* @param {number} x
* @return {boolean}
*/
var isPalindrome = function(x) {
if (x<0){
return false;
}else{
let oriString = x.toString();
let revString = oriString.split('').reverse().join('');
if (oriString == revString){
return true;
}else{
return false;
}
}

};

Runtime: 208 ms, faster than 35.50% of JavaScript online submissions for Palindrome Number.
Memory Usage: 48.4 MB, less than 47.41% of JavaScript online submissions for Palindrome Number.


If you like this blog or find it useful for you, you are welcome to comment on it. You are also welcome to share this blog, so that more people can participate in it. If the images used in the blog infringe your copyright, please contact the author to delete them. Thank you !